﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;
using PurchaseOrderExtra;

namespace PresentationLayer
{
    public partial class RaisePurchaseOrderList : System.Web.UI.Page
    {
        RaisePurchaseOrderControl control;
        User user;

        string supplierId;
        string supplierName;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId != 5)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                control = new RaisePurchaseOrderControl();

                List<string> userList = Session["UserLoggedIn"] as List<string>;
                string username = userList[3];
                user = control.GetUserByUsername(username);

                if (!IsPostBack)
                {
                    supplierId = "ALL";
                    supplierName = "All Suppliers";
                    List<string> searchInfo = new List<string>();
                    searchInfo.Add(supplierId);
                    searchInfo.Add(supplierName);
                    ViewState["RaisePO"] = searchInfo;

                    // Populate dropdownlist and GridView
                    PopulateSupplierName();
                    GenerateGridView();
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void PopulateSupplierName()
        {
            List<Supplier> list = control.DisplayAllSupplier();
            ddlSupplierName.DataSource = list;
            ddlSupplierName.DataTextField = "SupplierName";
            ddlSupplierName.DataValueField = "SupplierID";
            ddlSupplierName.DataBind();
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            try
            {
                supplierId = ddlSupplierName.SelectedValue;
                supplierName = ddlSupplierName.SelectedItem.Text;
                List<string> searchInfo = new List<string>();
                searchInfo.Add(supplierId);
                searchInfo.Add(supplierName);
                ViewState["RaisePO"] = searchInfo;

                GenerateGridView();
            }
            catch (Exception ex)
            {
            }
        }

        protected void lbtnViewDetail_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
                Label lblSupplierId = (Label)clickedRow.FindControl("lblSupplierIdGV");
                Label lblSupplierName = (Label)clickedRow.FindControl("lblSupplierNameGV");
                string supplierId = lblSupplierId.Text;
                string supplierName = lblSupplierName.Text;
                List<string> poInfo = new List<string>();
                poInfo.Add(supplierId);
                poInfo.Add(supplierName);

                // Save to HttpSession
                Session["SelectedRaisePO"] = poInfo;

                // Redirect to view details page
                string redirectURL = control.ViewRaisePurchaseOrder();
                Response.Redirect(redirectURL);
            }
            catch (Exception ex)
            {
            }
        }

        private void GenerateGridView()
        {
            lblMessage.Text = "";

            List<string> searchInfo = ViewState["RaisePO"] as List<string>;
            supplierId = searchInfo[0];
            supplierName = searchInfo[1];

            List<RaisePODisplay> list = new List<RaisePODisplay>();
            if (supplierId.Equals("ALL"))
            {
                list = control.GetRaisePODisplay(user);
            }
            else
            {
                RaisePODisplay raisePODisplay = control.GetRaisePODisplayByUserAndSupplierId(user, supplierId);
                if (raisePODisplay.Quantity > 0)
                {
                    list.Add(control.GetRaisePODisplayByUserAndSupplierId(user, supplierId));
                }
            }

            gvRaisePOList.DataSource = list;
            gvRaisePOList.DataBind();

            if (list.Count <= 0)
            {
                lblMessage.Text = "No item has been added for " + supplierName;
            }
        }

        protected void btnSelectMore_Click(object sender, EventArgs e)
        {
            try
            {
                string redirectURL = control.RedirectToSelectMoreItemsForPurchasing();
                Response.Redirect(redirectURL);
            }
            catch (Exception ex)
            {
            }
        }

    }
}